package com.slipper.system.codeless.controller;

import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.InterfaceFolderBO;
import com.slipper.system.codeless.bo.InterfaceBO;
import com.slipper.system.codeless.service.InterfaceFolderService;
import com.slipper.system.codeless.service.InterfaceService;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import com.slipper.util.BeanUtil;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 接口目录表
 */
@RestController
@RequestMapping("/system/interfaceFolder")
public class InterfaceFolderController {
    @Resource
    private InterfaceFolderService interfaceFolderService;
    @Resource
    private InterfaceService interfaceService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody InterfaceFolderBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(interfaceFolderService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody InterfaceFolderBO bo) {
        return ResultUtil.successResult(interfaceFolderService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody InterfaceFolderBO bo) {
        return ResultUtil.successResult(interfaceFolderService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody InterfaceFolderBO bo) {
        if (Tool.isBlank(bo.getParentId())) {
            bo.setParentId(Constants.ROOT_ID);
        }

        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(interfaceFolderService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody InterfaceFolderBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数错误！");
        }

        InterfaceFolderBO interfaceFolderBO = new InterfaceFolderBO();
        interfaceFolderBO.setParentId(bo.getId());
        if (interfaceFolderService.queryTotal(interfaceFolderBO) > 0) {
            return ResultUtil.failureResult("目录已被使用无法删除！");
        }

        InterfaceBO interfaceBO = new InterfaceBO();
        interfaceBO.setFolderId(bo.getId());
        if (interfaceService.queryTotal(interfaceBO) > 0) {
            return ResultUtil.failureResult("目录已被使用无法删除！");
        }
        
        return ResultUtil.deleteResult(interfaceFolderService.delete(bo));
    }


    @PostMapping("/queryTree")
    public String queryTree() {
        List<InterfaceFolderBO> folderList = interfaceFolderService.queryList(new InterfaceFolderBO());
        TreeInfo treeInfo = new TreeInfo("目录树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, BeanUtil.setOrdinal(folderList), "asc"));
    }

    @PostMapping(value = "/updateParentId")
    public String updateParentId(@RequestBody InterfaceFolderBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数错误！");
        }

        if (Tool.isBlank(bo.getParentId())) {
            bo.setParentId(Constants.ROOT_ID);
        }

        if (bo.getId().equals(bo.getParentId())) {
            return ResultUtil.failureResult("不能移动到此目录！");
        }

        if (interfaceFolderService.isParent(bo.getParentId(), bo.getId())) {
            return ResultUtil.failureResult("不允许移动到此子文件下");
        }

        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(interfaceFolderService.updateParentId(bo), bo);
    }
}
